<?php

/**
 * Description of ViewPanel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
abstract class ViewPanel extends ViewHtmlElement {

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {

        $fields['tag'] = 'div';
        $fields['element_id'] = $fields['panel']['name'] . '_panel';
        $panelTypes = ViewUtils::getPanelTypes();
        if (is_numeric($fields['panel']['panel_type'])) {
            $fields['class_array'] = array(
                'panel',
                $panelTypes[$fields['panel']['panel_type']]
            );
        }

        parent::__construct($fields);

        /**
         * Debugging mode.
         */
        if (DEBUGGING_MODE == ON) {
            $h4 = ViewHtmlElement::elementInit('h4', $this->getName());
            $this->addInnerHtml($h4);
            if (is_array($this->fields['panel']['articles'])) {
                if (count($this->fields['panel']['articles']) > 0) {
                    $ul = new ViewHtmlElement(array('tag' => 'ul'));
                    foreach ($this->fields['page']['panel']['articles'] as $article) {
                        $viewArticle = $this->getArticleInstance($article);
                        $ul->addInnerHtml($viewArticle->encapsulate('li'));
                    }
                    $this->addInnerHtml($ul);
                }
                else {
                    $p = ViewHtmlElement::elementInit('p', 'There are no articles.');
                    $this->addInnerHtml($p);
                }
            }
        }        

    }
    
    /**
     * Getting the URL of this page.
     * 
     * @return string
     */
    protected function getPageUrl() {
        return $this->fields['page']['full_url'];
    }
    
    /**
     * Getting the URL of the resource.
     * 
     * @return string
     */
    protected function getResource() {
        $protocol = $this->fields['page']['is_secure'] ? 'https://' : 'http://';
        return $protocol . $this->fields['page']['resource'];        
    }
    
    /**
     * Instance of an article.
     * 
     * @param array $article
     * @return mixed
     */
    protected function getArticleInstance($article) {
        return NULL;
    }
    
    /**
     * View fields to be passed to article view object.
     * 
     * @param array $article
     * @return array
     */
    protected function getArticleFields($article) {
        $output = $this->fields;
        unset($output['panel']['articles']);
        $output['panel']['article'] = $article;
        return $output;
    }
    
    /**
     * The final rendering. 
     */
    public function make() {
        
    }

}